"""Header_Information
------------------------------------
    File_Name: context
    Author: RR9
    Date: 2024/10/12
    Description: 上下文管理器
------------------------------------
"""
from contextlib import contextmanager
from app.database.OracleDBPool import OracleDBPool


class OracleContext:
    """oracle 连接数据库上下文管理"""

    def __init__(self, config: dict):
        print('OracleContext__init__')
        # 创建连接池
        self.pool = OracleDBPool(config).get_pool()

    @contextmanager
    def get_conn(self):
        # 分配一个会话
        conn = self.pool.acquire()
        try:
            print('OracleContext__enter__')
            yield conn
        except Exception as e:
            print('连接池分配会话出错')
            raise e
        finally:
            # 释放会话
            print('OracleContext__exit__')
            self.pool.close()
